const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const moment = require('../../../lib/moment/moment.min.js');
var app = getApp();
var page;
var that;
Page({
  data: {
    hasNotices: true,
    scrollLeft: 0,
    noticesByDays: [],
    newsTypes:{},
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    that = this;
    page = util.page.clone();
    that.setData({
      newsTypes: app.baseEnums.noticeTypes
    });
    that.toDoNotices();
  },
  onPullDownRefresh() {
    page.reset();
    that.setData({
      noticesByDays:[]
    });
    that.toDoNotices();
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    that.toDoNotices();
  },
  toDoNotices: function () { //查询通知
    let params = { newsType: 'notice'};
    params.isAllow = true;
      /**
       * [
          { time: '2018-7-24', notices: [{ imgs: [1, 2, 3, 4, 5, 6] }, { imgs: [] }] },
          { time: '2018-7-25', notices: [{ imgs: [1, 2, 3, 4, 5, 6] }, { imgs: [] }] }
         ]
       */
      app.loadPageExt(api.ToDoNotices, page, params).then(rsp => {
        Object.keys(rsp).forEach(function (key, i) {
          var itemArr = rsp[key];
          let tmp = {};
          tmp.createTime = key;
          itemArr.forEach(function (item, i) {
            item.createTime = moment(item.createTime).format('M月DD日 HH:mm');
            if (item.newsType == 'rentDatail'){
              var content = JSON.parse(item.content);
              content.totalCost = (content.totalCost/100).toFixed(2);
              item.content = content;
            } else if (item.newsType == 'entering'){
              if (item.isProcess){
                if (item.isAgreed) {
                  item.content = '恭喜您成为' + item.unitName + '号楼' + item.houseName + '租客，祝您入住愉快！';
                } else {
                  item.content = '很抱歉，您没有通过房东的认证，不能成为' + item.unitName + '号楼' + item.houseName + '租客，如有疑问请联系房东！'
                }
              }else{
                item.content = '申请成为' + item.unitName + '号楼' + item.houseName + '租客,请耐心等待房东审核';
              }
            } else if (item.newsType == 'withdrawal') {
              if (item.isProcess) {
                if (item.isAgreed) {
                  item.content = '恭喜您' + item.unitName + '号楼' + item.houseName + '退租申请成功！';
                } else {
                  item.content = '很抱歉，' + item.unitName + '号楼' + item.houseName +',您没有通过房东的退租申请,如有疑问请联系房东！';
                }
              } else {
                item.content = item.unitName + '号楼' + item.houseName + '申请退租,请耐心等待房东审核';
              }
            }
            if (!util.isNullOrEmpty(item.photoUrl)) {
              item.imgs = item.photoUrl.split(',').map(function (v, k) {
                return app.showImg(v);
              });
            } else {
              item.imgs = [];
            }
          });
          tmp.notices = itemArr;
          that.data.noticesByDays.push(tmp);
        });
        that.setData({ noticesByDays: that.data.noticesByDays });
      }).catch((err) => {
        console.log(err);
      });
  },
  onShow: function () {},
  previewCertImage: function (e) {
    let idx = e.currentTarget.dataset.idx;
    let inx = e.currentTarget.dataset.inx;
    let urls = this.data.noticesByDays[idx].notices[inx].imgs;
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
  previewImage: function (e) {
    let idx = e.currentTarget.dataset.idx;
    let inx = e.currentTarget.dataset.inx;
    let urls = this.data.noticesByDays[idx].notices[inx].imgs;
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
})